<?php
namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;

class House extends Controller
{

    /**
     * 查询所有房源信息
     */
    public function index()
    {
        $num = count(Db::name('houseinfo')->select());
        $result = Db::name('houseinfo')
//		    ->alias('a')
//			->join('housetypeinfo b','a.house_type = b.id')
			->paginate(2);
        $this->assign('num', $num);
        $this->assign('result', $result);
        return $this->fetch('house/houseinfo');
    }

    /**
     * 根据房源地区模糊查询
     */
    public function searchbyaddress()
    {
        $num = count(Db::name('houseinfo')->select());
        $address = $_GET['address'];
        $result = Db::name('houseinfo')->where('house_district', 'like', '%' . $address . '%')->paginate(2);
        $this->assign('result', $result);
        $this->assign('num', $num);
        return $this->fetch('house/houseinfo');
    }

    /**
     * 打开房源编辑页面
     */
    public function openhouseupdate()
    {
        $address = $_GET['houseid'];
        $result = Db::name('houseinfo')->where("id", $address)->find();
        $type = Db::name('housetypeinfo')->select();
        $this->assign('type',$type);
        $this->assign('result', $result);
        return $this->fetch('house/houseupdate');
    }

    /**
     * 打开房源添加页面
     */
    public function openhouseinsert()
    {
        $result = Db::name('housetypeinfo')->select();
        $this->assign('housetype', $result);
        return $this->fetch('house/houseinsert');
    }
	/*****************定义操作上传的函数************************/
  	function move_uploads($imgTmp,$imgName,$imgType){
	    //构造上传图片的名称
	    $upload_name = time().rand(1000000, 1000000000);
	    if($imgType == "image/jpeg"){
	        $filename = "./uploads/".$upload_name.".jpg";
	    }else{
	        $filename = "./uploads/".$upload_name.".png";
	    }
	    //上传操作
	    move_uploaded_file($imgTmp,$filename);
	    return $upload_name;
  	}
    /**
     * 添加房源
     */
    public function inserthouse(Request $request)
    {
    	//得到上传文件并检查是否满足条件（图片格式，图片大小）
		$img = $_FILES['house_smallimg'];
		//得到图片类型
		$imgType = $img['type'];
		//得到图片大小
		$imgSize = $img['size'];
		//得到图片临时存放位置名称
		$imgTmpName = $img['tmp_name'];
		  //得到图片的名称
		$imgName = $img['name'];
		  //1、检查是否存在存放上传文件的文件夹
		if(file_exists("./uploads")){
		    $imgnewName = $this-> move_uploads($imgTmpName,$imgName,$imgType);
		}else{
		    //如果检查到不存在uploads文件则创建目录
		    mkdir("./uploads");
		    $imgnewName = $this-> move_uploads($imgTmpName,$imgName,$imgType);
		}
		
		/******************************多图上传*********************/
		$files = $request->file('fileimg');
		$item = [];
		foreach($files as $file){
			$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
			if($info){
				$item[]=$info->getSaveName();
			}else{
				$this->error($file->getError());
			}
		}
		$arr = implode(',',$item);
        $house_smallimg = $imgnewName;//房源小图片
        $house_bigimg = $arr;//房源图片
        $house_num = $_POST['house_num'];//房源编号
        $house_title = $_POST['house_title'];//房源标题
        $house_isres = $_POST['house_isres'];//可注册
		$house_type = $_POST['house_type'];//房源类型
		$house_district = $_POST['house_district'];//房源地段
		$house_intro = $_POST['house_intro'];//房源简介
		$house_unitprice = $_POST['house_unitprice'];//房源每平方米单价
		$house_totalprices = $_POST['house_totalprices'];//房源总价
		$house_floorspace = $_POST['house_floorspace'];//房源占地面积
		$house_fitment = $_POST['house_fitment'];//房源装修程度
		$house_freetime = $_POST['house_freetime'];//免租时间
		$house_earliest = $_POST['house_earliest'];//最早可租
		$house_shortest = $_POST['house_shortest'];//最短租期
		$house_aareainfo = $_POST['house_aareainfo'];//面积信息
		$house_priceadv = $_POST['house_priceadv'];//价格优势
		$house_fitmentinfo = $_POST['house_fitmentinfo'];//装修程度详情
		$house_looktime = $_POST['house_looktime'];//看房时间
		$house_detailedaddress = $_POST['house_detailedaddress'];//房源详细地址
		$house_metro = $_POST['house_metro'];//到地铁的时间
		$house_brand = $_POST['house_brand'];//房源品牌
		$house_feature = $_POST['house_feature'];// 房源特色
		$house_state = 0;//房源状态
		$houseType = Db::name('housetypeinfo')->where('type_content',$house_type)->find()['id'];
		$data=[
		    'house_num' => $house_num,
		    'house_title' => $house_title,
		    'house_isres' => $house_isres,
		    'house_type' => $houseType,
		    'house_district' => $house_district,
		    'house_intro' => $house_intro,
		    'house_unitprice' => $house_unitprice, 
		    'house_totalprices' => $house_totalprices,
		    'house_floorspace' => $house_floorspace,
		    'house_fitment' => $house_fitment,
		    'house_freetime' => $house_freetime,
		    'house_earliest' => $house_earliest,
		    'house_shortest' => $house_shortest,
		    'house_aareainfo' => $house_aareainfo,
		    'house_priceadv' => $house_priceadv,
		    'house_fitmentinfo' => $house_fitmentinfo,
		    'house_looktime' => $house_looktime,
		    'house_detailedaddress' => $house_detailedaddress,
		    'house_metro' => $house_metro,
		    'house_brand' => $house_brand,
		    'house_feature' => $house_feature,
		    'house_state' => $house_state,
		    'house_smallimg' => $house_smallimg,
		    'house_bigimg' => $house_bigimg,
		];
		$result = Db::name('houseinfo')->insert($data);
		if($result>0){
		    return 1;
		}
    }
    /**
     * 修改房源
     */
    public function houseupdate(Request $request){
    	//得到上传文件并检查是否满足条件（图片格式，图片大小）
		$img = $_FILES['house_smallimg'];
		//得到图片类型
		$imgType = $img['type'];
		//得到图片大小
		$imgSize = $img['size'];
		//得到图片临时存放位置名称
		$imgTmpName = $img['tmp_name'];
		  //得到图片的名称
		$imgName = $img['name'];
		  //1、检查是否存在存放上传文件的文件夹
		if(file_exists("./uploads")){
		    $imgnewName = $this-> move_uploads($imgTmpName,$imgName,$imgType);
		}else{
		    //如果检查到不存在uploads文件则创建目录
		    mkdir("./uploads");
		    $imgnewName = $this-> move_uploads($imgTmpName,$imgName,$imgType);
		}
		
    	/******************************多图上传*********************/
		$files = $request->file('fileimg');
		$item = [];
		foreach($files as $file){
			$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
			if($info){
				$item[]=$info->getSaveName();
			}else{
				$this->error($file->getError());
			}
		}
		$arr = implode(',',$item);
        $house_bigimg = $arr;//房源图片
        $house_smallimg = $imgnewName;//房源小图片
    	$house_id = $_POST['house_id'];//房源id
    	$house_num = $_POST['house_num'];//房源编号
        $house_title = $_POST['house_title'];//房源标题
        $house_isres = $_POST['house_isres'];//可注册
		$house_type = $_POST['house_type'];//房源类型
		$house_district = $_POST['house_district'];//房源地段
		$house_intro = $_POST['house_intro'];//房源简介
		$house_unitprice = $_POST['house_unitprice'];//房源每平方米单价
		$house_totalprices = $_POST['house_totalprices'];//房源总价
		$house_floorspace = $_POST['house_floorspace'];//房源占地面积
		$house_fitment = $_POST['house_fitment'];//房源装修程度
		$house_freetime = $_POST['house_freetime'];//免租时间
		$house_earliest = $_POST['house_earliest'];//最早可租
		$house_shortest = $_POST['house_shortest'];//最短租期
		$house_aareainfo = $_POST['house_aareainfo'];//面积信息
		$house_priceadv = $_POST['house_priceadv'];//价格优势
		$house_fitmentinfo = $_POST['house_fitmentinfo'];//装修程度详情
		$house_looktime = $_POST['house_looktime'];//看房时间
		$house_detailedaddress = $_POST['house_detailedaddress'];//房源详细地址
		$house_metro = $_POST['house_metro'];//到地铁的时间
		$house_brand = $_POST['house_brand'];//房源品牌
		$house_feature = $_POST['house_feature'];// 房源特色
		$house_state = 0;//房源状态
//		$houseType = Db::name('housetypeinfo')->where('type_content',$house_type)->find()['id'];
		$data=[
		    'house_num' => $house_num,
		    'house_title' => $house_title,
		    'house_isres' => $house_isres,
		    'house_type' => $house_type,
		    'house_district' => $house_district,
		    'house_intro' => $house_intro,
		    'house_unitprice' => $house_unitprice, 
		    'house_totalprices' => $house_totalprices,
		    'house_floorspace' => $house_floorspace,
		    'house_fitment' => $house_fitment,
		    'house_freetime' => $house_freetime,
		    'house_earliest' => $house_earliest,
		    'house_shortest' => $house_shortest,
		    'house_aareainfo' => $house_aareainfo,
		    'house_priceadv' => $house_priceadv,
		    'house_fitmentinfo' => $house_fitmentinfo,
		    'house_looktime' => $house_looktime,
		    'house_detailedaddress' => $house_detailedaddress,
		    'house_metro' => $house_metro,
		    'house_brand' => $house_brand,
		    'house_feature' => $house_feature,
		    'house_state' => $house_state,
		    'house_smallimg' => $house_smallimg,
		    'house_bigimg' => $house_bigimg,
		];
		$result = Db::name('houseinfo')->where('id',$house_id)->update($data);
		if($result>0){
		    return 1;
		}
    }
    /**
     * 删除房源
     */
    public function housedelete(){
        $houseid = $_GET['houseid'];
        $result = Db::name('houseinfo')->where('id',$houseid)->delete();
        if($result>0){
        	$this->redirect('house/index');
        }
    }
    /**
     * 修改房源状态
     */
    public function houseupdatestate(){
        $houseid = $_GET['houseid'];
        $state = Db::name('houseinfo')->field('house_state')->where('id',$houseid)->find()['house_state'];
        if($state == 0){
        	$result = Db::name('houseinfo')->where('id',$houseid)->update(['house_state' =>1]);
        	if($result>0){
        		$this->redirect('house/index');
        	}
        }else if($state == 1){
        	$result = Db::name('houseinfo')->where('id',$houseid)->update(['house_state' =>0]);
        	if($result>0){
        		$this->redirect('house/index');
        	}
        }
    }
}

?>